package com.hrt.datasender.datacollect.drive.weigeng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.result.Row;
import com.btcode.schedule.ScheduleManager;
import com.hrt.datasender.datacollect.drive.DriveBase;
import com.hrt.datasender.datacollect.drive.DriveConfigModel;
import com.hrt.datasender.datacollect.drive.DriveParameterMetadata;
import com.hrt.datasender.datadao.model.BcDeviceConfigModel;
import com.hrt.datasender.datadao.model.BcDeviceMonitorParamModel;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.db.DBManager;

/**
 * 微耕门禁驱动
 * 
 * @author gislin
 *
 */
public class WeiGengDoorDrive extends DriveBase {

    @Override
    protected void beforeInit(BcDevicesModel device, List<BcDeviceMonitorParamModel> params,
            List<BcDeviceConfigModel> configs) {
        //initTable();
    }

    @Override
    public String getConsStr() {
        return "0/5 * * * * ?";
    }

    @Override
    public String getNote() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getDriveName() {
        return "微耕门禁驱动";
    }

    /**
     * 初始化数据表
     */
    private void initTable() {
        IDataExecuter sqlletExecuter = DBManager.getDataExecuter();
        String tabesql = "SELECT * FROM sqlite_master WHERE type='table' AND name ='BD_DOOR_ACCESS'";

        List<Row> rows = sqlletExecuter.queryList(tabesql, null);

        if (rows != null && rows.size() > 0) {
            log.info("BD_DOOR_ACCESS表已存在！");
        }
        else {
            String sql = " CREATE TABLE BD_DOOR_ACCESS(" + "ID TEXT PRIMARY KEY NOT NULL," + "CONTROL_NO TEXT,"
                    + "DOOR_NO TEXT," + "RECORD_NO TEXT," + "RECORD_TYPE TEXT," + "RECORD_RESULT TEXT,"
                    + "IN_OR_OUT TEXT," + "CAR_NO TEXT," + "RECORD_REASON TEXT," + "RECORD_DATETIME TEXT,"
                    + "DOOR_MAGNETISM TEXT" + ")";

            sqlletExecuter.create(sql, null);
            log.info("表BD_DOOR_ACCESS创建成功！");
        }
    }

    @Override
    public List<DriveParameterMetadata> getParameterMetadata() {
        List<DriveParameterMetadata> params = new ArrayList<>();
        params.add(new DriveParameterMetadata("ip", "IP"));
        params.add(new DriveParameterMetadata("port", "端口"));
        params.add(new DriveParameterMetadata("controlNo", "控制器序列号"));
        return params;
    }

    @Override
    protected void registerMQListener(BcDevicesModel device, HashMap<Integer, String> paramCodeMap,
            DriveConfigModel configs) {

    }

    @Override
    protected void registerAskDataJob(BcDevicesModel device, DriveConfigModel configs) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("deviceConfigs", configs);
        data.put("device", device);
        String jobId = getClass().getName() + "." + configs.get("controlNo");
        ScheduleManager.getInstance().addJob(WeiGengDataAsker.class, getConsStr(), data, jobId);
    }

}
